﻿@model List<TaskExecutionLog>

@{
    ViewData["Title"] = "Execution Logs";
    var totalPages = (int)Math.Ceiling((double)ViewBag.TotalCount / ViewBag.PageSize);
}

<h2>Execution Logs for @ViewBag.TaskName</h2>

<div class="mb-3">
    <form asp-action="ClearLogs" asp-route-id="@ViewBag.TaskId" method="post">
        <button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure you want to clear all logs for this task?')">Clear All Logs</button>
    </form>
</div>

<table class="table table-striped table-bordered">
    <thead class="thead-dark">
        <tr>
            <th>Start Time</th>
            <th>Duration</th>
            <th>Status</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var log in Model)
        {
            <tr>
                <td>@log.StartTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
                <td>@((log.EndTime - log.StartTime)?.ToString(@"hh\:mm\:ss\.fff"))</td>
                <td>
                    @if (log.IsSuccess)
                    {
                        <span class="badge rounded-pill bg-success" style="font-size: 0.9em; padding: 5px 10px;">Success</span>
                    }
                    else
                    {
                        <span class="badge rounded-pill bg-danger" style="font-size: 0.9em; padding: 5px 10px;">Failed</span>
                    }

                </td>
                <td>
                    <button class="btn btn-sm btn-info" data-toggle="modal" data-target="#logModal"
                            data-logid="@log.Id">
                        View Details
                    </button>
                </td>
            </tr>
        }
    </tbody>
</table>

<div class="modal fade" id="logModal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content" id="logModalContent">
            <!-- Content will be loaded via AJAX -->
        </div>
    </div>
</div>

@if (totalPages > 1)
{
    <nav>
        <ul class="pagination">
            @for (int i = 1; i <= totalPages; i++)
            {
                <li class="page-item @(i == ViewBag.Page ? "active" : "")">
                    <a class="page-link" asp-action="ExecutionLogs" asp-route-id="@ViewBag.TaskId" asp-route-page="@i">@i</a>
                </li>
            }
        </ul>
    </nav>
}

<div>
    <a asp-action="Index" class="btn btn-secondary">Back to List</a>
</div>

<!-- 视图中的 Scripts 部分 -->
@section Scripts {
    <script>
        $(document).ready(function() {
            // 直接绑定按钮点击事件，手动控制模态框
            $('[data-target="#logModal"]').click(function(e) {
                e.preventDefault();
                var logId = $(this).data('logid');
                var url = '@Url.Action("LogDetails")/' + logId;

                // 先加载内容，再显示模态框
                $('#logModalContent').load(url, function() {
                    $('#logModal').modal('show');
                });
            });
        });
    </script>
}